<?php
/**
 * Created by PhpStorm.
 * User: sunbo
 * Date: 2019/7/3
 * Time: 9:47 AM
 */
namespace app\index\controller;
use think\Exception;
class System extends CoinBaseContorller
{
    /**
     *
     * @api {post} api/system/gettoken  web总代币数量
     * @apiDescription  web总代币数量
     * @apiGroup system
     * @apiSuccess {string}  cust_sum_count 代币总数量
     * @apiSuccess {string}  cust_name 代币名
     * @apiSuccess {string}  usdt_name USDT
     * @apiSuccess {string}  usdt_sum_count USDT总数量
     * @apiSuccess {string}  usdt_today_token_count USDT今日新增数量
     * @apiVersion 1.0.1
     */
    public  function  gettoken(){
         $userguid=$this->getuserGuid();
         $data=$this->_gettoken($userguid);
         return $this->json_data($data,"");
    }

    function _gettoken($userguid){
        //获取代币 总数
      $row = $this->db_select("select IFNULL(sum(token_count)/POW(10,8),0) counts from token_user_custom_coin_balance");
      $count=$row[0]["counts"];

      $row=$this->GetCoinInfo("1");
      $coinname=$row[0]["name"];

      //获取USDT 收入总数
      $row = $this->db_select("select IFNULL(SUM(a.tokencount),0) putin_token_count from token_bill_transaction_coin a INNER JOIN token_sys_coin b on b.iscustom=0 and b.guid=a.token_sys_coin_guid WHERE a.type=0 and a.status=1");
      $putin_token_count=$row[0]["putin_token_count"];

      //获取USDT 支出总数
        $row = $this->db_select("select IFNULL(SUM(a.tokencount),0) sellout_token_count  from token_bill_transaction_coin a INNER JOIN token_sys_coin b on b.iscustom=0 and b.guid=a.token_sys_coin_guid WHERE a.type=1 and a.status=1");
        $sellout_token_count=$row[0]["sellout_token_count"];

        //获取USDT 当日收入
        $row = $this->db_select("select IFNULL(SUM(a.tokencount),0) today_token_count from token_bill_transaction_coin a INNER JOIN token_sys_coin b on b.iscustom=0 and b.guid=a.token_sys_coin_guid WHERE a.type=0 and a.status=1 and a.billdate='".date('Y-m-d')."'");
        $today_token_count=$row[0]["today_token_count"];

        return  array("cust_sum_count"=>$count,"cust_name"=>$coinname,"usdt_name"=>"USDT","usdt_sum_count"=>$putin_token_count-$sellout_token_count,"usdt_today_token_count"=>$today_token_count) ;
    }



    /**
     *
     * @api {post} api/system/coin_to_coin  币转币配置
     * @apiDescription  币转币配置
     * @apiGroup system
     * @apiSuccess {string}  src_coin_name  源币种
     * @apiSuccess {string}  tar_coin_name  目标币种
     * @apiSuccess {string}  ratio  兑换比例 1：N
     * @apiSuccess {string}  fee  手续费
     * @apiVersion 1.0.1
     */
    public  function  coin_to_coin(){
          $data=$this->_coin_to_coin();
          return $this->json_data($data,"");
    }

    function  _coin_to_coin(){
       return  $this->db_select("select a.guid,a.ratio,a.src_token_sys_coin_guid,a.target_token_sys_coin_guid,a.fee,b.name src_coin_name,c.name tar_coin_name from token_sys_coin_exchange_coin a inner join token_sys_coin b on b.guid=a.src_token_sys_coin_guid
INNER JOIN token_sys_coin c on c.guid=a.target_token_sys_coin_guid");
    }

    /**
     *
     * @api {post} api/system/save_coin_to_coin 保存币转币配置
     * @apiDescription  保存币转币配置
     * @apiGroup system
     * @apiParam {string} guid guid
     * @apiParam {string} ratio  兑换比例
     * @apiParam {string} fee    手续费
     * @apiSuccess {string}  data  成功返回空字符 刷新
     * @apiVersion 1.0.1
     */
    public  function  save_coin_to_coin(){
        $guid=$this->param("guid");
        $ratio=$this->param("ratio");
        $fee=$this->param("fee");
        try{
            $this->_save_coin_to_coin($guid,$ratio,$fee);
            return $this->json_data("","");
        }catch (Exception $e){
            return $this->json_data("",$e->getMessage());
        }
    }

    function  _save_coin_to_coin($guid,$ratio,$fee){
       return  $this->db_exec(array("update token_sys_coin_exchange_coin set ratio=".$ratio.", fee=".$fee." WHERE guid='".$guid."'"));
    }

    /**
     *
     * @api {post} api/system/get_ListEarningsDetail 获取收益配置列表
     * @apiDescription  获取收益配置列表
     * @apiGroup system
     * @apiSuccess {string}  name  收益类型
     * @apiSuccess {string}  ratio 收益率
     * @apiVersion 1.0.1
     */
    public  function  get_ListEarningsDetail(){
        $data=$this->_get_List_Earnings_Detail();
        return $this->json_data($data,"");
    }


    function  _get_List_Earnings_Detail(){
        return $this->db_select("select a.guid,a.title name,a.ratio from token_sys_earnings_detail a ");
    }


    /**
     *
     * @api {post} api/system/ModifyEarningsDetail 修改收益配置
     * @apiDescription  修改收益配置
     * @apiGroup system
     * @apiParam {string} guid guid
     * @apiParam {string} ratio  兑换比例
     * @apiVersion 1.0.1
     */
    public  function  ModifyEarningsDetail(){
        $guid=$this->param("guid");
        $name=$this->param("name");
        $ratio=$this->param("ratio");
        $data=$this->_ModifyEarningsDetail($guid,$ratio,$name);
        return $this->json_data($data,"");
    }

    public  function  _ModifyEarningsDetail($guid,$ratio,$name){
        return $this->db_exec(array("UPDATE token_sys_earnings_detail set ratio=".$ratio.",title='".$name."'  WHERE guid='".$guid."'"));
    }

    /**
     *
     * @api {post} api/system/SystemUserList Web用户列表
     * @apiDescription  Web用户列表
     * @apiGroup system
     * @apiSuccess {string}  guid  guid
     * @apiSuccess {string} phone  手机号
     * @apiSuccess {string} name  名称
     * @apiVersion 1.0.1
     */
    public function SystemUserList(){
        $row=$this->db_select("select guid,phone,name from token_sys_user WHERE issystem=1 and phone <> 'admin'");
        return $this->json_data($row,"");
    }

    /**
     *
     * @api {post} api/system/Sys_DeleteUser Web删除用户
     * @apiDescription  Web删除用户
     * @apiGroup system
     * @apiParam {string} guid guid
     * @apiSuccess {string}  data  空为成功
     * @apiVersion 1.0.1
     */
    public function Sys_DeleteUser(){
        $guid=$this->param("guid");
        $row=$this->db_exec(array("delete from token_sys_user where guid='".$guid."'"));
        return $this->json_data($row,"");
    }

    /**
     *
     * @api {post} api/system/Sys_UpdateUser Web修改用户
     * @apiDescription  Web修改用户
     * @apiGroup system
     * @apiParam {string} guid guid
     * @apiParam {string} phone phone
     * @apiParam {string} name name
     * @apiSuccess {string}  data  空为成功
     * @apiVersion 1.0.1
     */
    public function Sys_UpdateUser(){
        $guid=$this->param("guid");
        $phone=$this->param("phone");
        $name=$this->param("name");
        $row=$this->db_exec(array("update  token_sys_user set phone='".$phone."',name='".$name."' where guid='".$guid."'"));
        return $this->json_data($row,"");
    }


    /**
     *
     * @api {post} api/system/Sys_UpdateUserStatus Web用户-状态
     * @apiDescription  修改Web用户-状态
     * @apiGroup system
     * @apiParam {string} status 0 禁用 1 允许
     * @apiParam {string} guid  用户
     * @apiSuccess {string}  data  空为成功
     * @apiVersion 1.0.1
     */
    public function Sys_UpdateUserStatus(){
        $status=$this->param("status");
        $guid=$this->param("guid");
        $row=$this->db_exec(array("update  token_sys_user set isallow='".$status."' where guid='".$guid."'"));
        return $this->json_data($row,"");
    }



    /**
     *
     * @api {post} api/system/Sys_InsertUser Web新增用户
     * @apiDescription  Web新增用户
     * @apiGroup system
     * @apiParam {string} phone phone
     * @apiParam {string} name name
     * @apiParam {string} pwd md5(密码)
     * @apiSuccess {string}  data  空为成功
     * @apiVersion 1.0.1
     */
    public function Sys_InsertUser(){
        $phone=$this->param("phone");
        $name=$this->param("name");
        $pwd=$this->param("pwd");

        $row=$this->db_select("select count(*) counts from token_sys_user where phone='".$phone."' and issystem=1");
        if($row[0]["counts"]>0){
            return $this->json_data("","手机号已经存在");
        }

        $row=$this->db_exec(array("insert into token_sys_user(guid,name,phone,pwd,issystem,token_sys_admin_role_guid)VALUES(UUID(),'".$name."','".$phone."','".$pwd."',1,'2')"));
        return $this->json_data($row,"");
    }


    /**
     *
     * @api {post} api/system/Sys_FeeList Web矿工费列表
     * @apiDescription  Web矿工费列表
     * @apiGroup system
     * @apiSuccess {string}  guid  guid
     * @apiSuccess {string}  coinname  代币名称
     * @apiSuccess {string}  fee  转出手续费
     * @apiVersion 1.0.1
     */
    public  function  Sys_FeeList(){
       $row= $this->db_select("select a.guid,a.fee/power(10,8) fee,b.name coinname from token_sys_coin_fee a INNER JOIN token_sys_coin b on b.guid=a.token_sys_coin_guid");
       return $this->json_data($row,"");
    }


    /**
     *
     * @api {post} api/system/Sys_UpdateFee Web修改矿工费
     * @apiDescription  Web修改矿工费
     * @apiGroup system
     * @apiParam {string}  guid  guid
     * @apiParam {string}  fee
     * @apiSuccess {string}  data  空为成功
     * @apiVersion 1.0.1
     */
    public  function  Sys_UpdateFee(){
       $guid= $this->param("guid");
       $fee= $this->param("fee")*pow(10,8);
       $row= $this->db_exec(array("update token_sys_coin_fee set fee=".$fee." where guid='".$guid."'"));
       return $this->json_data($row,"");
    }


    /**
     *
     * @api {post} api/system/Sys_ConfigList Web系统参数列表
     * @apiDescription  Web系统参数列表
     * @apiGroup system
     * @apiSuccess {string}  guid  guid
     * @apiSuccess {string}  name  关键字
     * @apiSuccess {string}  title 描述
     * @apiSuccess {string}  value 值
     * @apiVersion 1.0.1
     */
    public  function  Sys_ConfigList(){
        $row= $this->db_select("select guid,value,title name,sys_describe title  from token_sys_config");
        return $this->json_data($row,"");
    }


    /**
     *
     * @api {post} api/system/Sys_InsertConfig Web系统参数新增
     * @apiDescription  Web系统参数新增
     * @apiGroup system
     * @apiParam {string}  name  关键字
     * @apiParam {string}  title 描述
     * @apiParam {string}  value 值
     * @apiVersion 1.0.1
     */
    public  function  Sys_InsertConfig(){
        $name=$this->param("name");
        $title=$this->param("title");
        $value=$this->param("value");
       $row= $this->db_select("select count(*) counts from token_sys_config where name='".$name."'");
       if($row[0]["counts"]>0){
           return $this->json_data("","关键字已经存在");
       }
        $row= $this->db_exec(array("insert into  token_sys_config (guid,name,title,sys_describe,value) values(uuid(),'".$name."','".$name."','".$title."','".$value."')"));
        return $this->json_data($row,"");
    }


    /**
     *
     * @api {post} api/system/Sys_UpdateConfig Web系统参数更新
     * @apiDescription  Web系统参数更新
     * @apiGroup system
     * @apiParam {string}  guid  guid
     * @apiParam {string}  title 描述
     * @apiParam {string}  value 值
     * @apiVersion 1.0.1
     */
    public  function  Sys_UpdateConfig(){
        $guid=$this->param("guid");
        $title=$this->param("title");
        $name=$this->param("name");
        $value=$this->param("value");

        $row= $this->db_exec(array("update token_sys_config set sys_describe='".$title."',title='".$name."', value='".$value."' where guid='".$guid."'"));
        return $this->json_data($row,"");
    }

    /**
     *
     * @api {post} api/system/Sys_DeleteConfig Web系统参数删除
     * @apiDescription  Web系统参数删除
     * @apiGroup system
     * @apiParam {string}  guid  guid
     * @apiVersion 1.0.1
     */
    public  function  Sys_DeleteConfig(){
        $guid=$this->param("guid");
        $row= $this->db_exec(array("delete from  token_sys_config  where guid='".$guid."'"));
        return $this->json_data($row,"");
    }


}
